package kuba.aspects;

import java.util.logging.Level;

import kuba.bean.Author;
import kuba.bean.CD;
import kuba.common.BeanLogger;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Aspects {
    
    
    //Author
    @Pointcut("execution(* kuba.daos.AuthorDAO.insert(..))")
    public void pointcutAuthorDaoInsert() {
        // Do nothing because it's pointcut
    }
    
    @Around("pointcutAuthorDaoInsert()")
    public void aroundAuthorInsert(ProceedingJoinPoint joinPoint) {
        Author author = (Author)joinPoint.getArgs()[0];
        BeanLogger.getLOGGER().info("Before Author insert: " +author);
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
        
    }

    @After("pointcutAuthorDaoInsert()")
    public void afterAuthorInsert() {
        BeanLogger.getLOGGER().info("Author inserted");
    }
    

    @Pointcut("execution(* kuba.daos.AuthorDAO.delete(..))")
    public void pointcutAuthorDaoDelete() {
        // Do nothing because it's pointcut
    }
    
    @Around("pointcutAuthorDaoDelete()")
    public void aroundAuthorDelete(ProceedingJoinPoint joinPoint) {
        Author author = (Author)joinPoint.getArgs()[0];
        BeanLogger.getLOGGER().info("Before Author delete: " +author);
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
        
    }

    @After("pointcutAuthorDaoDelete()")
    public void afterAuthorDelete() {
        BeanLogger.getLOGGER().info("Author deleted");
    }

    @Pointcut("execution(* kuba.daos.AuthorDAO.update(..))")
    public void pointcutAuthorDaoUpdate() {
        // Do nothing because it's pointcut
    }
    
    @Around("pointcutAuthorDaoUpdate()")
    public void aroundAuthorUpdate(ProceedingJoinPoint joinPoint) {
        Author author = (Author)joinPoint.getArgs()[0];
        BeanLogger.getLOGGER().info("Before Author update: " +author);
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
        
    }

    @After("pointcutAuthorDaoUpdate()")
    public void afterAuthorUpdate() {
        BeanLogger.getLOGGER().info("Author updated");
    }
    
    
    //CD
    @Pointcut("execution(* kuba.daos.CdDAO.insert(..))")
    public void pointcutCdDaoInsert() {
        // Do nothing because it's pointcut
    }
    
    @Around("pointcutCdDaoInsert()")
    public void aroundCdInsert(ProceedingJoinPoint joinPoint) {
        CD cd = (CD)joinPoint.getArgs()[0];
        BeanLogger.getLOGGER().info("Before CD insert: " +cd);
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
        
    }

    @After("pointcutCdDaoInsert()")
    public void afterCdInsert() {
        BeanLogger.getLOGGER().info("CD inserted");
    }
    
    @Pointcut("execution(* kuba.daos.CdDAO.delete(..))")
    public void pointcutCdDaoDelete() {
        // Do nothing because it's pointcut
    }
    
    @Around("pointcutCdDaoDelete()")
    public void aroundCdDelete(ProceedingJoinPoint joinPoint) {
        CD cd = (CD)joinPoint.getArgs()[0];
        BeanLogger.getLOGGER().info("Before CD delete: " +cd);
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
        
    }

    @After("pointcutCdDaoDelete()")
    public void afterCdDelete() {
        BeanLogger.getLOGGER().info("CD deleted");
    }
    
    @Pointcut("execution(* kuba.daos.CdDAO.update(..))")
    public void pointcutCdDaoUpdate() {
        // Do nothing because it's pointcut
    }
    
    @Around("pointcutCdDaoUpdate()")
    public void aroundCdUpdate(ProceedingJoinPoint joinPoint) {
        CD cd = (CD)joinPoint.getArgs()[0];
        BeanLogger.getLOGGER().info("Before CD update: " +cd);
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
        
    }

    @After("pointcutCdDaoUpdate()")
    public void afterCdUpdate() {
        BeanLogger.getLOGGER().info("CD updated");
    }
    
}
